package com.crm.approval.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.FinanceApply;
import com.crm.model.SysDuty;
import com.crm.model.SysUser;
import com.crm.service.FinanceApplyService;
import com.crm.service.SysDutyService;
import com.crm.service.SysUserService;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/applied")
public class FinanceAppliedController {
	@Autowired
	private FinanceApplyService financeApplyService;
	@Autowired
	SysDutyService dutyService;
	@Autowired
	SysUserService userService;
	@RequestMapping("/index")
	public String index(HttpServletRequest request){
		SysUser sysUser= ((SysUser)SessionUtils.get(SessionModel.SESSION_USER));
		request.setAttribute("user", sysUser);
		return "approval/applied";
	}
	@RequestMapping("/selectAllByWhere")
	@ResponseBody
	public void selectAllByWhere(HttpServletResponse response,HttpServletRequest request,Page<FinanceApply> page,FinanceApply t){
		t.setApplicantId(getApplicantIdsStr());
		Page<FinanceApply> list = (Page<FinanceApply>) financeApplyService.selectAllByWhere(page, t);
		ResponseUtil.resultPage(response,request,list);
	}
	@RequestMapping("/getTotalMoneyByWhere")
	@ResponseBody
	public Map<String,Object> getTotalMoneyByWhere(FinanceApply t){
		Map<String,Object> retMap = new HashMap<>();
		t.setApplicantId(getApplicantIdsStr());
		int totalMoney = financeApplyService.getTotalMoneyByWhere(t);
		retMap.put("data", totalMoney);
		return retMap;
	}
	/**
	 * 获取当前用户及其职位以下的所有用户
	 * @return
	 */
	private String getApplicantIdsStr(){
		StringBuilder sb = new StringBuilder();
		List<String> userIdS = new ArrayList<>();
		List<String> dutyIds = new ArrayList<>();
		SysUser user =(SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		String dutyId = user.getDutyId();
		if(dutyId==null) return null;
		userIdS.add(user.getUserId());
		List<SysDuty> dutyList =dutyService.selectAll(new Page<SysDuty>());
		for(SysDuty duty:dutyList){
			if(dutyId.equals(duty.getParentId())){
				dutyIds.add(duty.getDutyId());
			}
		}
		getChild( dutyIds,dutyList);
		if(dutyIds.size()>0)userIdS.addAll(userService.selectUsersByDutyIds(dutyIds));
		for(String userId:userIdS){
			sb.append(userId).append(",");
		}
		return sb.deleteCharAt(sb.lastIndexOf(",")).toString();
	}
	
	private void getChild(List<String> dutyIds,List<SysDuty> dutyList){
		List<String> dutyIdList = new ArrayList<>();
		for(String dutyId:dutyIds){
			for(SysDuty duty:dutyList){
				if(dutyId.equals(duty.getParentId())){
					dutyIdList.add(dutyId);
				}
			}
			dutyIds.addAll(dutyIdList);
			getChild(dutyIdList,dutyList);
		}
	}
}
